#include<iostream>

using namespace std;

const int N = 1e6 + 10;
int n;
int l[N] , r[N];
int dfs(int root)
{
    if(!root) return 0;
    int a = dfs(l[root]);
    int b = dfs(r[root]);
    return max(a,b) + 1;
}
int main()
{
    cin >> n;
    for(int i = 1;i <= n;i ++)
    {
        cin >> l[i] >> r[i];
    }
    cout << dfs(1) << endl;
    return 0;
}